<template>
  <div class="login-form-container">
    <!-- 背景装饰元素 -->
    <div class="background-decoration">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-blur blur-1"></div>
    </div>
    
    <div class="login-form-wrapper">
      <!-- Logo区域 -->
      <div class="logo-section">
        <img src="/logo.svg" alt="MateChat" class="logo">
        <h1 class="app-title">MateChat</h1>
        <p class="app-subtitle">智能对话助手</p>
      </div>
      
      <div class="login-form">
        <div class="form-header">
          <h2>{{ $t('auth.loginTitle') }}</h2>
          <p>{{ $t('auth.loginSubtitle') }}</p>
        </div>
        
        <form @submit.prevent="handleLogin" class="form-content">
          <div class="form-group">
            <d-input
              v-model="formData.email"
              type="email"
              :placeholder="$t('auth.emailPlaceholder')"
              :error="errors.email"
              size="lg"
              prefix-icon="email"
              required
            >
              <template #prefix>
                <d-icon name="email" size="16px" />
              </template>
            </d-input>
            <div v-if="errors.email" class="error-message">
              <d-icon name="error" size="14px" />
              {{ errors.email }}
            </div>
          </div>
          
          <div class="form-group">
            <d-input
              v-model="formData.password"
              type="password"
              :placeholder="$t('auth.passwordPlaceholder')"
              :error="errors.password"
              size="lg"
              prefix-icon="lock"
              show-password
              required
            >
              <template #prefix>
                <d-icon name="lock" size="16px" />
              </template>
            </d-input>
            <div v-if="errors.password" class="error-message">
              <d-icon name="error" size="14px" />
              {{ errors.password }}
            </div>
          </div>
          
          <div class="form-options">
            <d-checkbox v-model="rememberMe" size="sm">
              {{ $t('auth.rememberMe') }}
            </d-checkbox>
            <a href="#" class="forgot-password">{{ $t('auth.forgotPassword') }}</a>
          </div>
          
          <d-button
            type="submit"
            variant="solid"
            color="primary"
            :loading="authStore.isLoading"
            :disabled="!isFormValid"
            size="lg"
            class="login-button"
          >
            <template #icon>
              <d-icon name="login" size="16px" />
            </template>
            {{ $t('auth.loginButton') }}
          </d-button>
          
          <div class="divider">
            <span>{{ $t('auth.orLoginWith') }}</span>
          </div>
          
          <div class="social-login">
            <d-button variant="outline" size="lg" class="social-button">
              <template #icon>
                <d-icon name="github" size="18px" />
              </template>
              GitHub
            </d-button>
            <d-button variant="outline" size="lg" class="social-button">
              <template #icon>
                <d-icon name="google" size="18px" />
              </template>
              Google
            </d-button>
          </div>
        </form>
        
        <div class="form-footer">
          <p>
            {{ $t('auth.noAccount') }}
            <a href="#" @click.prevent="$emit('switchToRegister')" class="link">
              {{ $t('auth.registerNow') }}
            </a>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, computed, ref } from 'vue';
import { useAuthStore } from '@/store';
import type { LoginRequest } from '@/types/auth';

const emit = defineEmits<{
  switchToRegister: [];
  loginSuccess: [];
}>();

const authStore = useAuthStore();
const rememberMe = ref(false);

const formData = reactive<LoginRequest>({
  email: '',
  password: '',
});

const errors = reactive({
  email: '',
  password: '',
});

const isFormValid = computed(() => {
  return formData.email && formData.password && !errors.email && !errors.password;
});

const validateForm = () => {
  errors.email = '';
  errors.password = '';
  
  if (!formData.email) {
    errors.email = '请输入邮箱地址';
    return false;
  }
  
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  if (!emailRegex.test(formData.email)) {
    errors.email = '请输入有效的邮箱地址';
    return false;
  }
  
  if (!formData.password) {
    errors.password = '请输入密码';
    return false;
  }
  
  if (formData.password.length < 6) {
    errors.password = '密码长度至少6位';
    return false;
  }
  
  return true;
};

const handleLogin = async () => {
  if (!validateForm()) {
    return;
  }
  
  const result = await authStore.login(formData);
  
  if (result.success) {
    emit('loginSuccess');
  } else {
    errors.password = result.error || '登录失败，请重试';
  }
};
</script>

<style scoped lang="scss">
@import "devui-theme/styles-var/devui-var.scss";

.login-form-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
  background: linear-gradient(135deg, $devui-brand 0%, $devui-brand-active 100%);
  position: relative;
  overflow: hidden;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  
  .decoration-circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    animation: float 6s ease-in-out infinite;
  }
  
  .circle-1 {
    width: 200px;
    height: 200px;
    top: 10%;
    right: 10%;
    animation-delay: 0s;
  }
  
  .circle-2 {
    width: 150px;
    height: 150px;
    bottom: 15%;
    left: 5%;
    animation-delay: 2s;
  }
  
  .decoration-blur {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.05);
    filter: blur(40px);
  }
  
  .blur-1 {
    width: 300px;
    height: 300px;
    top: -50px;
    right: -50px;
  }
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.login-form-wrapper {
  width: 100%;
  max-width: 440px;
  z-index: 1;
}

.logo-section {
  text-align: center;
  margin-bottom: 40px;
  
  .logo {
    width: 80px;
    height: 80px;
    margin-bottom: 16px;
    filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.15));
  }
  
  .app-title {
    margin: 0 0 8px 0;
    font-size: 32px;
    font-weight: 700;
    color: white;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  }
  
  .app-subtitle {
    margin: 0;
    font-size: 16px;
    color: rgba(255, 255, 255, 0.8);
    font-weight: 400;
  }
}

.login-form {
  background: $devui-base-bg;
  border-radius: 24px;
  padding: 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.form-header {
  text-align: center;
  margin-bottom: 32px;
  
  h2 {
    margin: 0 0 8px 0;
    font-size: 28px;
    font-weight: 600;
    color: $devui-text;
    background: linear-gradient(135deg, $devui-brand 0%, $devui-brand-active 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
  }
  
  p {
    margin: 0;
    color: $devui-text-weak;
    font-size: 14px;
    line-height: 1.5;
  }
}

.form-content {
  .form-group {
    margin-bottom: 24px;
    
    :deep(.devui-input) {
      width: 100%;
      border-radius: 12px;
      transition: all 0.3s ease;
      
      &:hover {
        border-color: $devui-brand;
        box-shadow: 0 0 0 3px rgba($devui-brand, 0.1);
      }
      
      &:focus-within {
        border-color: $devui-brand;
        box-shadow: 0 0 0 3px rgba($devui-brand, 0.2);
      }
    }
    
    .error-message {
      display: flex;
      align-items: center;
      gap: 6px;
      margin-top: 8px;
      font-size: 12px;
      color: $devui-danger;
      font-weight: 500;
    }
  }
  
  .form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    
    :deep(.devui-checkbox) {
      font-size: 14px;
      color: $devui-text-weak;
    }
    
    .forgot-password {
      color: $devui-brand;
      text-decoration: none;
      font-size: 14px;
      font-weight: 500;
      
      &:hover {
        text-decoration: underline;
      }
    }
  }
  
  .login-button {
    width: 100%;
    height: 48px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 24px;
    transition: all 0.3s ease;
    
    &:hover:not(:disabled) {
      transform: translateY(-2px);
      box-shadow: 0 8px 20px rgba($devui-brand, 0.3);
    }
  }
  
  .divider {
    position: relative;
    text-align: center;
    margin: 24px 0;
    color: $devui-text-weak;
    font-size: 14px;
    
    &::before {
      content: '';
      position: absolute;
      top: 50%;
      left: 0;
      right: 0;
      height: 1px;
      background: $devui-dividing-line;
    }
    
    span {
      background: $devui-base-bg;
      padding: 0 16px;
      position: relative;
      z-index: 1;
    }
  }
  
  .social-login {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 8px;
    
    .social-button {
      height: 44px;
      border-radius: 12px;
      border-color: $devui-dividing-line;
      color: $devui-text;
      font-weight: 500;
      transition: all 0.3s ease;
      
      &:hover {
        border-color: $devui-brand;
        color: $devui-brand;
        transform: translateY(-1px);
      }
    }
  }
}

.form-footer {
  margin-top: 24px;
  text-align: center;
  padding-top: 24px;
  border-top: 1px solid $devui-dividing-line;
  
  p {
    margin: 0;
    font-size: 14px;
    color: $devui-text-weak;
  }
  
  .link {
    color: $devui-brand;
    text-decoration: none;
    font-weight: 600;
    
    &:hover {
      text-decoration: underline;
    }
  }
}

@media screen and (max-width: 480px) {
  .login-form {
    padding: 32px 24px;
    border-radius: 20px;
  }
  
  .logo-section {
    .app-title {
      font-size: 28px;
    }
    
    .app-subtitle {
      font-size: 14px;
    }
  }
  
  .social-login {
    grid-template-columns: 1fr;
  }
}

@media screen and (max-width: 360px) {
  .login-form {
    padding: 24px 20px;
  }
  
  .form-header h2 {
    font-size: 24px;
  }
}
</style>